package com.hht.toolanduilibs.wb

import android.annotation.SuppressLint
import android.content.Context
import android.net.Uri
import android.os.Environment
import android.provider.Settings
import com.hht.toolanduilibs.*
import com.hht.toolanduilibs.util.GsonHelp
import java.io.File
import java.io.FileInputStream
import java.io.InputStreamReader
import java.nio.charset.StandardCharsets


/**
 * Author:chen'hu
 * Time:2024/9/4 10:51
 * Description:This is WhiteBoardHelp
 */
@SuppressLint("NewApi")
class WhiteBoardHelp(private val mContext: Context) {
    private var annotationBean: AnnotationBean =
        AnnotationBean(0, "com.hht.annotation", "com.hht.annotation.AnnotationService", false)
    private var whiteBoardBean: WhiteBoardBean = WhiteBoardBean(0,
        "tw.com.newline.whiteboard",
        "tw.com.newline.whiteboard.android.MainActivity",
        true,
        "android.hht.action.whiteboard",
        "filePath")
    private var mConfigurationBean: ConfigurationBean? = null

    private var mWhiteBoardFactory: WhiteBoardFactory? = null

    private val jsonPath = "/etc/Whiteboard/whiteboard.json"

    private var isExternalStorageManager = false

    private var whiteBoardUtils: WhiteBoardUtils? = null

    companion object {
        @SuppressLint("StaticFieldLeak")
        private var instance: WhiteBoardHelp? = null

        fun getInstall(context: Context): WhiteBoardHelp {
            if (null == instance) {
                synchronized(this) {
                    if (null == instance) {
                        instance = WhiteBoardHelp(context.applicationContext)
                    }
                }
            }
            return instance!!
        }
    }


    init {
        initConfigurationJson()
    }

    private fun initConfigurationJson() {

        if (!Environment.isExternalStorageManager()) {
            mContext.startActivityExt {
                action = Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION
                data = Uri.parse("package:${mContext.packageName}")
            }
        } else {
            isExternalStorageManager = true
            try {
                FileInputStream(jsonPath).use { fis ->
                    InputStreamReader(fis, StandardCharsets.UTF_8).use { reader ->
                        val jsonString = reader.readText()
                        "whiteboard==$jsonString".showLog(1)
                        mConfigurationBean = GsonHelp.newInstance()
                            .stringToBean(jsonString, ConfigurationBean::class.java)
                    }
                }
            } catch (e: Exception) {
                whiteBoardUtils = WhiteBoardUtils(mContext)
                isExternalStorageManager = false
                e.toString().showLog()
            } finally {
                if (null == mConfigurationBean) {
                    mConfigurationBean = ConfigurationBean(whiteBoardBean, annotationBean)
                }
                mWhiteBoardFactory = WhiteBoardFactory(mConfigurationBean!!, mContext)
            }
        }
    }

    /**
     * 打开批注
     * */
    fun openAnnotation(): Boolean {
        if (!isExternalStorageManager) {
            initConfigurationJson()
        }
        return if (mWhiteBoardFactory == null) {
            false
        } else {
            if (isExternalStorageManager) {
                return mWhiteBoardFactory!!.openAnnotation()
            } else {
                whiteBoardUtils?.startAnnotation()
                return true
            }
        }
    }

    /**
     *  打开白板
     * */
    fun openWhiteBoard(): Boolean {
        if (!isExternalStorageManager) {
            initConfigurationJson()
        }
        return if (mWhiteBoardFactory == null) {
            false
        } else {
            if (isExternalStorageManager) {
                mWhiteBoardFactory!!.openWhiteBoard()
            } else {
                whiteBoardUtils?.startWhiteBoard()
                return true
            }
        }
    }

    /**
     * @param file 向白板传图片
     * */
    fun sendPicturesToWhiteBoard(file: File?): Boolean {
        if (!isExternalStorageManager) {
            initConfigurationJson()
        }
        return if (mWhiteBoardFactory == null) {
            false
        } else {
            if (isExternalStorageManager) {
                mWhiteBoardFactory!!.sendPicturesToWhiteBoard(file)
            } else {
                if (file == null) {
                    whiteBoardUtils?.startWhiteBoard()
                    return false
                } else {
                    whiteBoardUtils?.openWhiteBoardAndUir(file)
                    return true
                }
            }
        }
    }
}